package com.btpn.custody.entity;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;

import com.btpn.infra.entity.AEntityLong;

@Entity
@org.hibernate.annotations.Entity(dynamicInsert=true, dynamicUpdate=true)
@Table(name = "M_BRANCH")
@NamedQueries({
	@NamedQuery(name  = "Branch.findAll",
				query = "FROM Branch b ORDER BY b.name"),

	@NamedQuery(name  = "Branch.getByCode",
				query = "FROM Branch b WHERE b.code = :branchCode"),

	@NamedQuery(name  = "Branch.getByName",
			query = "FROM Branch b WHERE b.name = :branchName"),

	@NamedQuery(name  = "Branch.findByAreaId",
				query = "FROM Branch b WHERE b.area.id = :areaId ORDER BY b.code"),

	@NamedQuery(name  = "Branch.findNotById",
				query = "FROM Branch b WHERE b.id IS NOT :id")
})
public class Branch extends AEntityLong implements Comparable<Branch> {

	/*=============================================== C O N S T A N T =========================================*/
	private static final long serialVersionUID = 6098658891935143479L;

	public static final String NQ__FIND_ALL		= "Branch.findAll";

	public static final String NQ__GET_BY__CODE = "Branch.getByCode";

	public static final String NQ__GET_BY__NAME = "Branch.getByName";

	public static final String NQ__FIND_BY__AREA_ID = "Branch.findByAreaId";

	public static final String NQ__FIND_NOT_BY__ID = "Branch.findNotById";

	/*=============================================== M E M B E R =============================================*/
	@Column(name = "BRANCH_CODE", unique=true)
	private String code;

	@Column(name = "BRANCH_NAME")
	private String name;

	@Column(name = "BRANCH_EMAIL")
	private String email;

	@ManyToOne()
	@JoinColumn(name="AREA_ID")
	private Area area;

	// unused?
	@OneToMany(cascade = {CascadeType.REFRESH}, mappedBy = "branch")
//	@Cascade(org.hibernate.annotations.CascadeType.REPLICATE)
	private List<MasterData> masterDatas = new ArrayList<MasterData>(0);

	/*=============================================== O V E R R I D E    M E T H O D ==========================*/
	/*
     * The Comparable interface is only allow to sort a single property.
     * To sort with multiple properties, you need Comparator.
     */
    public static Comparator<Branch> BranchComparator = new Comparator<Branch>() {

        public int compare(Branch fruit1, Branch fruit2) {
            String fruitName1 = fruit1.getName().toUpperCase();
            String fruitName2 = fruit2.getName().toUpperCase();

            //ascending order
            return fruitName1.compareTo(fruitName2);

            //descending order
            //return fruitName2.compareTo(fruitName1);
        }
    };

    @Override
	public int compareTo(Branch o) {
		// undefined

		return 0;
	}

	/*=============================================== G E T T E R   &   S E T T E R ===========================*/
	public void setCode(String code) {
		this.code = code;
	}

	public String getCode() {
		return code;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public Area getArea() {
		return area;
	}

	public void setArea(Area area) {
		this.area = area;
	}

	public List<MasterData> getMasterDatas() {
		return masterDatas;
	}

	public void setMasterDatas(List<MasterData> masterDatas) {
		this.masterDatas = masterDatas;
	}
}
